package com.yupi.yuoj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yupi.yuoj.common.ErrorCode;
import com.yupi.yuoj.exception.ThrowUtils;
import com.yupi.yuoj.model.dto.contentcomment.ContentCommentQueryRequest;
import com.yupi.yuoj.model.entity.ContentComment;
import com.yupi.yuoj.model.entity.User;
import com.yupi.yuoj.model.vo.ContentCommentVO;
import com.yupi.yuoj.model.vo.UserVO;
import com.yupi.yuoj.service.ContentCommentService;
import com.yupi.yuoj.mapper.ContentCommentMapper;
import com.yupi.yuoj.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author admin
* @description 针对表【content_comment(内容评论表)】的数据库操作Service实现
* @createDate 2025-11-01 21:28:52
*/
@Service
public class ContentCommentServiceImpl extends ServiceImpl<ContentCommentMapper, ContentComment>
    implements ContentCommentService{

    @Resource
    private UserService userService;

    @Override
    public void validContentComment(ContentComment contentComment, boolean add) {
        if (contentComment == null) {
            throw new RuntimeException("评论不能为空");
        }
        
        Long contentId = contentComment.getContent_id();
        Long userId = contentComment.getUser_id();
        String commentText = contentComment.getComment_text();
        
        // 创建时，参数不能为空
        if (add) {
            ThrowUtils.throwIf(contentId == null || contentId <= 0, ErrorCode.PARAMS_ERROR, "内容ID不能为空");
            ThrowUtils.throwIf(userId == null || userId <= 0, ErrorCode.PARAMS_ERROR, "用户ID不能为空");
            ThrowUtils.throwIf(StringUtils.isBlank(commentText), ErrorCode.PARAMS_ERROR, "评论内容不能为空");
        }
        
        // 校验评论内容长度
        if (StringUtils.isNotBlank(commentText) && commentText.length() > 500) {
            throw new RuntimeException("评论内容过长");
        }
    }

    @Override
    public QueryWrapper<ContentComment> getQueryWrapper(ContentCommentQueryRequest contentCommentQueryRequest) {
        QueryWrapper<ContentComment> queryWrapper = new QueryWrapper<>();
        if (contentCommentQueryRequest == null) {
            return queryWrapper;
        }
        
        Long id = contentCommentQueryRequest.getId();
        Long contentId = contentCommentQueryRequest.getContentId();
        Long userId = contentCommentQueryRequest.getUserId();
        Long parentId = contentCommentQueryRequest.getParentId();
        Integer status = contentCommentQueryRequest.getStatus();
        String sortField = contentCommentQueryRequest.getSortField();
        String sortOrder = contentCommentQueryRequest.getSortOrder();
        
        // 拼接查询条件
        queryWrapper.eq(id != null, "id", id);
        queryWrapper.eq(contentId != null, "content_id", contentId);
        queryWrapper.eq(userId != null, "user_id", userId);
        queryWrapper.eq(parentId != null, "parent_id", parentId);
        queryWrapper.eq(status != null, "status", status);
        
        // 排序规则
        queryWrapper.orderBy(StringUtils.isNotBlank(sortField),
                sortOrder.equals("ascend"), sortField);
        
        return queryWrapper;
    }

    @Override
    public ContentCommentVO getContentCommentVO(ContentComment contentComment, HttpServletRequest request) {
        ContentCommentVO contentCommentVO = ContentCommentVO.objToVo(contentComment);
        
        // 关联查询用户信息
        Long userId = contentComment.getUser_id();
        if (userId != null && userId > 0) {
            User user = userService.getById(userId);
            if (user != null) {
                UserVO userVO = userService.getUserVO(user);
                contentCommentVO.setUser(userVO);
            }
        }
        
        return contentCommentVO;
    }

    @Override
    public Page<ContentCommentVO> getContentCommentVOPage(Page<ContentComment> contentCommentPage, HttpServletRequest request) {
        List<ContentComment> contentCommentList = contentCommentPage.getRecords();
        Page<ContentCommentVO> contentCommentVOPage = new Page<>(contentCommentPage.getCurrent(), contentCommentPage.getSize(), contentCommentPage.getTotal());
        if (contentCommentList.isEmpty()) {
            return contentCommentVOPage;
        }
        
        // 填充信息
        List<ContentCommentVO> contentCommentVOList = contentCommentList.stream()
                .map(contentComment -> getContentCommentVO(contentComment, request))
                .collect(Collectors.toList());
        
        contentCommentVOPage.setRecords(contentCommentVOList);
        return contentCommentVOPage;
    }
}




